package com.softserve.academy.food.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;


import com.softserve.academy.food.entity.eHistoryOrders;
import com.softserve.academy.food.entity.eOrder;
import com.softserve.academy.food.entity.eUser;

import java.util.ArrayList;
import java.util.List;

@Repository
public class HibernateHistoryOrder extends Dao
{
	public eHistoryOrders Create(eHistoryOrders order) 
	{
		if (order!=null)
		{
			order.setId( (Integer)sessionFactory.getCurrentSession().save( order ) );
		}	
		return order;
	}
	
	
	public void Delete(int id)  
	{
		Query query;
		query = sessionFactory.getCurrentSession().createQuery("delete from eHistoryOrders where id = " + id);
		query.executeUpdate();
	}

	@SuppressWarnings("unchecked")
	public eHistoryOrders Read(eHistoryOrders order)  
	{
		int id = order.getId();
		
		List<eHistoryOrders> list = sessionFactory.getCurrentSession().createQuery( "from eHistoryOrders where id = "+id ).list();
		
		if (list.isEmpty())
		{
			return order;
		}
		
		return list.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public eOrder Read(int id)  
	{
		eOrder order = new eOrder();
		
		List<eOrder> list = sessionFactory.getCurrentSession().createQuery("from eHistoryOrders where id = "+id).list();
		
		if (list.isEmpty())
		{
			return order;
		}
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	public ArrayList<eHistoryOrders> ReadAll()  
	{
		return (ArrayList<eHistoryOrders>) sessionFactory.getCurrentSession().createQuery("from eHistoryOrders").list();
	}

	@SuppressWarnings("unchecked")
	public ArrayList<eHistoryOrders> getUser( eUser user )
	{
		return (ArrayList<eHistoryOrders>) sessionFactory.getCurrentSession()
				.createQuery("from eHistoryOrders where USER_USER_ID = "+user.getId()).list();
	}

}
